import Player from "../model/player/player.js";
import DateUtil from "../util/date.js";
import REDIS from "../util/redis.js";



export default class Work {
	/**
	 * 
	 * @param {Player} player 
	 */
	async start(player: Player) {
		if (await this.isWorking(player)) return {
			msg: `你已经在打工了`
		};
		if (!player.isAlive()) return {
			msg: `死人无法打工`
		};

		let nowSecondStamp = new DateUtil().secondStamp;
		await REDIS.set(`LNE:records:work:${player.qqNum}`, nowSecondStamp);
		return { msg: `开始打工了，记得来结束打工哦` };
	}
	/**
	 * 
	 * @param {Player} player 
	 */
	async stop(player: Player) {
		if (! await this.isWorking(player)) return {
			msg: `你没在打工`
		};
		if (!player.isAlive()) return {
			msg: `死人无法打工`
		};

		let startSS = Number.parseInt(await REDIS.get(`LNE:records:work:${player.qqNum}`));
		let nowSS = new DateUtil().secondStamp;
		let workSeconds = nowSS - startSS;
		const MAX_WORK_TIME = 4 * 60 * 60;
		if (workSeconds > MAX_WORK_TIME) workSeconds = MAX_WORK_TIME;
		await REDIS.set(`LNE:records:work:${player.qqNum}`, 0);

		let workHours = workSeconds / 60 / 60;
		let coinWage = Math.floor(workHours * this.hourCoinsFor(player));
		let expWage = Math.floor(workHours * this.hourExpFor(player));

		player.coin += coinWage;
		player.exp += expWage;

		workHours = Math.floor(workHours * 100) / 100;
		return { msg: `你一共打工了${workHours}小时\n获得了${coinWage}硬币和${expWage}经验` };
	}

	/**
	 * 
	 * @param {Player} player 
	 */
	hourCoinsFor(player: { level: number; }) {
		return 100 * (player.level + 1);
	}

	/**
	 * 
	 * @param {Player} player 
	 */
	hourExpFor(player: { level: number; }) {
		return 5 * (player.level + 1);
	}

	/**
	 * 
	 * @param {Player} player 
	 */
	async isWorking(player: { qqNum: any; }) {
		let startTime = await REDIS.get(`LNE:records:work:${player.qqNum}`);
		return startTime != null && startTime != 0;
	}
}